package com.hethongviet.job.helpers;

import com.hethongviet.job.AppMain;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SMSReceiver extends BroadcastReceiver
{
	/**
	 * The Action fired by the Android-System when a SMS was received. We are
	 * using the Default Package-Visibility
	 */
	@Override
	public void onReceive(Context context, Intent intent)
	{
		AppMain.debug("message -----------------------Received Broadcast!");
		if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
		{
			/* The SMS-Messages are 'hiding' within the extras of the Intent. */
			Bundle bundle = intent.getExtras();
			if (bundle != null)
			{
				/* Get all messages contained in the Intent */
				SmsMessage[] messages = null;
				// messages =
				// Telephony.Sms.Intents.getMessagesFromIntent(intent);
				Object[] pdusObj = (Object[]) bundle.get("pdus");
				messages = new SmsMessage[pdusObj.length];
				for (int i = 0; i < pdusObj.length; i++)
				{
					messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
				}
				if(messages.length > 0)
				{
					AppMain.receiveMessage(messages);
				}
			}
		}
	}
}
